package entities.forum;

import entities.User;
import entities.reports.ReportedTopic;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * Trabalho PD - 2014/2015
 * @author a21210380 a21210392
 */
@Entity
@Table(name = "topic")
@NamedQueries({
    @NamedQuery(name = "Topic.findAll", query = "SELECT t FROM Topic t"),
    @NamedQuery(name = "Topic.findByIdTopic", query = "SELECT t FROM Topic t WHERE t.idTopic = :idTopic"),
    @NamedQuery(name = "Topic.findByTitle", query = "SELECT t FROM Topic t WHERE t.title = :title"),
    @NamedQuery(name = "Topic.findByMessage", query = "SELECT t FROM Topic t WHERE t.message = :message"),
    @NamedQuery(name = "Topic.findByCreatedDate", query = "SELECT t FROM Topic t WHERE t.createdDate = :createdDate"),
    @NamedQuery(name = "Topic.findRecent", query = "SELECT t FROM Topic t ORDER BY t.createdDate DESC"),
    @NamedQuery(name = "Topic.findByLastInsertedPostNUmber", query = "SELECT t FROM Topic t WHERE t.lastInsertedPostNUmber = :lastInsertedPostNUmber")})
public class Topic implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idTopic")
    private Integer idTopic;
    
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "title")
    private String title;
    
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 700)
    @Column(name = "message")
    private String message;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "createdDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdDate;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "lastInsertedPostNUmber")
    private int lastInsertedPostNUmber;
    
    @JoinColumn(name = "idUser", referencedColumnName = "idUser")
    @ManyToOne(optional = false)
    private User idUser;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idTopic")
    private Collection<Post> posts;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idTopic")
    private Collection<ReportedTopic> reports;

    public Topic() {
    }
    
    public Topic(User idUser, String title, String message, Date createdDate) {
        this.idUser = idUser;
        this.title = title;
        this.message = message;
        this.createdDate = createdDate;
        this.lastInsertedPostNUmber = 0;
        this.posts = new ArrayList<>();
        this.reports = new ArrayList<>();
    }

    public Integer getId() {
        return idTopic;
    }
    
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Date getCreatedDate() {
        return createdDate;
    }
    
    public int getLastInsertedPostNUmber() {
        return lastInsertedPostNUmber;
    }

    public void incrementInsertedPostNUmber() {
        ++lastInsertedPostNUmber;
    }

    public User getUser() {
        return idUser;
    }
    
    public Collection<Post> getPosts() {
        return posts;
    }

    public void adddPost(Post post) {
        this.posts.add(post);
    }

    public Collection<ReportedTopic> getReportedTopicCollection() {
        return reports;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idTopic != null ? idTopic.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Topic)) {
            return false;
        }
        Topic other = (Topic) object;
        return !((this.idTopic == null && other.idTopic != null) || (this.idTopic != null && !this.idTopic.equals(other.idTopic)));
    }

    @Override
    public String toString() {
        return "entities.extra.Topic[ idTopic=" + idTopic + " ]";
    }

}
